package com.itljm.cxrMarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itljm.cxrMarket.common.PageResult;
import com.itljm.cxrMarket.common.Result;
import com.itljm.cxrMarket.entity.HygieneViolations;
import com.itljm.cxrMarket.entity.dto.ReasonRequestBody;
import com.itljm.cxrMarket.service.HygieneViolationsService;
import com.itljm.cxrMarket.service.SysUserService;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author ljm
 * @date 2025/5/19 15:57
 */
@RestController
@RequestMapping("/hygieneViolations")
public class HygieneViolationsController {
    @Autowired
    private HygieneViolationsService hygieneViolationsService;

    // 分页查询
    @RequestMapping("/info")
    public PageResult<HygieneViolations> findHygieneViolationsByPage(
            @RequestParam(required = false, defaultValue = "0") Integer page,
            @RequestParam(required = false, defaultValue = "15") Integer limit,
            @RequestParam(required = false) String merchantId,
            @RequestParam(required = false) String violationType,
            @RequestParam(required = false) Integer severity
    ) {
        Page<HygieneViolations> infoPage = new Page<>(page, limit);
        QueryWrapper<HygieneViolations> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(merchantId)) {
            wrapper.eq("hv.merchant_id", merchantId);
        }
        if (StringUtils.isNotEmpty(violationType)) {
            wrapper.eq("hv.violation_type", violationType);
        }
        if (severity != null) {
            wrapper.eq("hv.severity", severity);
        }

        List<HygieneViolations> list = hygieneViolationsService.findViolationsByPage(infoPage, wrapper);
        return new PageResult<>(list, infoPage.getTotal());
    }

    // 新增违规记录
    @PostMapping
    public Result addHygieneViolations(@RequestBody HygieneViolations info) {
        info.setCreateTime(new Date());
        info.setUpdateTime(new Date());
        info.setStatus(0);
        info.setProcessLog(info.getOperator() + " 创建了记录;");
        return hygieneViolationsService.save(info) ? Result.ok() : Result.fail("新增失败");
    }

    // 删除违规记录
    @DeleteMapping("/{id}")
    public Result deleteHygieneViolations(@PathVariable Integer id) {
        return hygieneViolationsService.removeById(id) ? Result.ok() : Result.fail("删除失败");
    }

    // 修改违规记录
    @PutMapping
    public Result updateHygieneViolations(@RequestBody HygieneViolations hygieneViolations) {
        return hygieneViolationsService.updateById(hygieneViolations) ? Result.ok() : Result.fail("修改失败");
    }

    // 根据id查询违规记录
    @GetMapping("/{id}")
    public Result getHygieneViolationsById(@PathVariable Integer id) {
        HygieneViolations info = hygieneViolationsService.findViolationsById(id);
        if (info == null) {
            return Result.fail("查询失败");
        }
        return Result.ok(info);
    }

    // 提交整改结果
    @PutMapping("/changes")
    public Result submitHygieneViolations(@RequestBody HygieneViolations hygieneViolations) {
        hygieneViolations.setStatus(1);
        hygieneViolations.setProcessLog(hygieneViolations.getProcessLog() + hygieneViolations.getOperator()+" 提交整改结果;");
        return hygieneViolationsService.updateById(hygieneViolations) ? Result.ok() : Result.fail("提交失败");
    }

    // 审批违规记录
    @PutMapping("/audit/{id}/{status}")
    public Result approveHygieneViolations(@PathVariable("id") Integer id, @PathVariable("status") Integer status,
                                           @RequestBody ReasonRequestBody requestBody) {
        if (requestBody == null || StringUtils.isEmpty(requestBody.getReason())) {
            return Result.fail("审批失败，原因不能为空");
        }
        HygieneViolations hygieneViolations = hygieneViolationsService.getById(id);
        if (status == 3) {
            hygieneViolations.setStatus(status);
            hygieneViolations.setProcessLog(hygieneViolations.getProcessLog() + requestBody.getOperator()+" 审批驳回, 原因："+ requestBody.getReason()+";");
        } else if (status == 2) {
            hygieneViolations.setStatus(status);
            hygieneViolations.setProcessLog(hygieneViolations.getProcessLog() + requestBody.getOperator()+" 审批通过, 意见："+ requestBody.getReason()+";");
        }
        return hygieneViolationsService.updateById(hygieneViolations) ? Result.ok() : Result.fail("审批失败");
    }

}
